const {
    PrintConfig
} = require('../models')
const logger = require('../utils/logger')

/**
 * 初始化打印配置数据
 */
async function initPrintConfigs() {
    try {
        logger.info('Initializing print configurations...')

        // 纸张大小配置
        const paperSizeConfigs = [{
                name: 'A4',
                category: 'paper_size',
                value: 'A4',
                display_name: 'A4 (210×297mm)',
                description: '标准A4纸张',
                price_multiplier: 1.0,
                points_multiplier: 1.0,
                sort_order: 1,
                is_default: true,
                is_active: true
            },
            {
                name: 'A3',
                category: 'paper_size',
                value: 'A3',
                display_name: 'A3 (297×420mm)',
                description: '大尺寸A3纸张',
                price_multiplier: 2.0,
                points_multiplier: 2.0,
                sort_order: 2,
                is_default: false,
                is_active: true
            },
            {
                name: 'A5',
                category: 'paper_size',
                value: 'A5',
                display_name: 'A5 (148×210mm)',
                description: '小尺寸A5纸张',
                price_multiplier: 0.7,
                points_multiplier: 0.7,
                sort_order: 3,
                is_default: false,
                is_active: true
            }
        ]

        // 颜色类型配置
        const colorTypeConfigs = [{
                name: '黑白',
                category: 'color_type',
                value: 'black_white',
                display_name: '黑白打印',
                description: '单色黑白打印',
                price_multiplier: 1.0,
                points_multiplier: 1.0,
                sort_order: 1,
                is_default: true,
                is_active: true
            },
            {
                name: '彩色',
                category: 'color_type',
                value: 'color',
                display_name: '彩色打印',
                description: '全彩色打印',
                price_multiplier: 3.0,
                points_multiplier: 3.0,
                sort_order: 2,
                is_default: false,
                is_active: true
            }
        ]

        // 打印质量配置
        const printQualityConfigs = [{
                name: '标准',
                category: 'print_quality',
                value: 'standard',
                display_name: '标准质量',
                description: '日常使用的标准打印质量',
                price_multiplier: 1.0,
                points_multiplier: 1.0,
                sort_order: 1,
                is_default: true,
                is_active: true
            },
            {
                name: '高质量',
                category: 'print_quality',
                value: 'high',
                display_name: '高质量',
                description: '更清晰的高质量打印',
                price_multiplier: 1.5,
                points_multiplier: 1.5,
                sort_order: 2,
                is_default: false,
                is_active: true
            },
            {
                name: '草稿',
                category: 'print_quality',
                value: 'draft',
                display_name: '草稿质量',
                description: '快速打印，节省墨水',
                price_multiplier: 0.8,
                points_multiplier: 0.8,
                sort_order: 3,
                is_default: false,
                is_active: true
            }
        ]

        // 装订方式配置
        const bindingTypeConfigs = [{
                name: '无装订',
                category: 'binding_type',
                value: 'none',
                display_name: '无装订',
                description: '不进行装订',
                price_multiplier: 1.0,
                points_multiplier: 1.0,
                sort_order: 1,
                is_default: true,
                is_active: true
            },
            {
                name: '左侧装订',
                category: 'binding_type',
                value: 'left_binding',
                display_name: '左侧装订',
                description: '左侧打孔装订',
                price_multiplier: 1.2,
                points_multiplier: 1.2,
                sort_order: 2,
                is_default: false,
                is_active: true
            },
            {
                name: '顶部装订',
                category: 'binding_type',
                value: 'top_binding',
                display_name: '顶部装订',
                description: '顶部打孔装订',
                price_multiplier: 1.2,
                points_multiplier: 1.2,
                sort_order: 3,
                is_default: false,
                is_active: true
            }
        ]

        // 合并所有配置
        const allConfigs = [
            ...paperSizeConfigs,
            ...colorTypeConfigs,
            ...printQualityConfigs,
            ...bindingTypeConfigs
        ]

        // 批量插入配置
        for (const config of allConfigs) {
            const existingConfig = await PrintConfig.findOne({
                where: {
                    category: config.category,
                    value: config.value
                }
            })

            if (!existingConfig) {
                await PrintConfig.create(config)
                logger.info(`Created print config: ${config.category} - ${config.value}`)
            } else {
                logger.info(`Print config already exists: ${config.category} - ${config.value}`)
            }
        }

        logger.info('Print configurations initialized successfully')
    } catch (error) {
        logger.error('Failed to initialize print configurations:', error)
        throw error
    }
}

// 如果直接运行此脚本
if (require.main === module) {
    const {
        sequelize
    } = require('../models')

    sequelize.sync().then(() => {
        return initPrintConfigs()
    }).then(() => {
        logger.info('Print configuration initialization completed')
        process.exit(0)
    }).catch((error) => {
        logger.error('Print configuration initialization failed:', error)
        process.exit(1)
    })
}

module.exports = initPrintConfigs